package com.jike.noobmoney.mvp.presenter;


import com.image.imagepicker.bean.ImageItem;
import com.jike.noobmoney.entity.BaseEntity;
import com.jike.noobmoney.entity.UploadFileEntity;
import com.jike.noobmoney.entity.UploadFilesEntity;
import com.jike.noobmoney.net.BaseSubscriber;
import com.jike.noobmoney.net.IView;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;


/**
 * App
 */
public class AppPresenter extends BasePresenter<IView> {

    public AppPresenter(IView view) {
        super(view);
    }

    /**
     * 上传单张图片 用户发布任务接口uploadfile2
     */
    public void upload(File file, final String type) {
        MultipartBody.Builder builder = new MultipartBody.Builder().setType(MultipartBody.FORM);
        RequestBody imageBody = RequestBody.create(MediaType.parse("multipart/form-data"), file);
        builder.addFormDataPart("file", file.getName(), imageBody);
        List<MultipartBody.Part> parts = builder.build().parts();
        addSubscription(iHttpUrl.upload2(parts), new BaseSubscriber<UploadFileEntity>() {
            @Override
            public void onNext(UploadFileEntity entity) {
                if (100 == entity.getRecode()) {
                    iView.onSuccess("" + entity.getRecode(), entity.getFilepath(), entity.getFilepath(), type);
                } else {
                    iView.onFailed("" + entity.getRecode(), entity.getFilepath());
                }
            }

            @Override
            public void onFailed(String code, String msg) {
                iView.onFailed(code, msg);
            }
        });
    }

    //水印
    public void upload3(File file, final String type) {
        MultipartBody.Builder builder = new MultipartBody.Builder().setType(MultipartBody.FORM);
        RequestBody imageBody = RequestBody.create(MediaType.parse("multipart/form-data"), file);
        builder.addFormDataPart("file", file.getName(), imageBody);
        List<MultipartBody.Part> parts = builder.build().parts();
        addSubscription(iHttpUrl.upload3(parts), new BaseSubscriber<UploadFileEntity>() {
            @Override
            public void onNext(UploadFileEntity entity) {
                if (100 == entity.getRecode()) {
                    iView.onSuccess("" + entity.getRecode(), entity.getFilepath(), entity.getFilepath(), type);
                } else {
                    iView.onFailed("" + entity.getRecode(), entity.getFilepath());
                }
            }

            @Override
            public void onFailed(String code, String msg) {
                iView.onFailed(code, msg);
            }
        });
    }
    //水印修改
    public void markuploadfile(File file, final String type) {
        MultipartBody.Builder builder = new MultipartBody.Builder().setType(MultipartBody.FORM);
        RequestBody imageBody = RequestBody.create(MediaType.parse("multipart/form-data"), file);
        builder.addFormDataPart("file", file.getName(), imageBody);
        List<MultipartBody.Part> parts = builder.build().parts();
        addSubscription(iHttpUrl.markuploadfile(parts), new BaseSubscriber<UploadFileEntity>() {
            @Override
            public void onNext(UploadFileEntity entity) {
                if (100 == entity.getRecode()) {
                    iView.onSuccess("" + entity.getRecode(), entity.getFilepath(), entity.getFilepath(), type);
                } else {
                    iView.onFailed("" + entity.getRecode(), entity.getFilepath());
                }
            }

            @Override
            public void onFailed(String code, String msg) {
                iView.onFailed(code, msg);
            }
        });
    }


    //上传头像文件
    public void singleuploadfile(File file, final String type) {
        MultipartBody.Builder builder = new MultipartBody.Builder().setType(MultipartBody.FORM);
        RequestBody imageBody = RequestBody.create(MediaType.parse("multipart/form-data"), file);
        builder.addFormDataPart("file", file.getName(), imageBody);
        List<MultipartBody.Part> parts = builder.build().parts();
        addSubscription(iHttpUrl.singleuploadfile(parts), new BaseSubscriber<UploadFileEntity>() {
            @Override
            public void onNext(UploadFileEntity entity) {
                if (100 == entity.getRecode()) {
                    iView.onSuccess("" + entity.getRecode(), entity.getFilepath(), entity.getFilepath(), type);
                } else {
                    iView.onFailed("" + entity.getRecode(), entity.getFilepath());
                }
            }

            @Override
            public void onFailed(String code, String msg) {
                iView.onFailed(code, msg);
            }
        });
    }


    //上传二维码和任务提交截图
    public void submituploadfile(File file, final String type) {
        MultipartBody.Builder builder = new MultipartBody.Builder().setType(MultipartBody.FORM);
        RequestBody imageBody = RequestBody.create(MediaType.parse("multipart/form-data"), file);
        builder.addFormDataPart("file", file.getName(), imageBody);
        List<MultipartBody.Part> parts = builder.build().parts();
        addSubscription(iHttpUrl.submituploadfile(parts), new BaseSubscriber<UploadFileEntity>() {
            @Override
            public void onNext(UploadFileEntity entity) {
                if (100 == entity.getRecode()) {
                    iView.onSuccess("" + entity.getRecode(), entity.getFilepath(), entity.getFilepath(), type);
                } else {
                    iView.onFailed("" + entity.getRecode(), entity.getFilepath());
                }
            }

            @Override
            public void onFailed(String code, String msg) {
                iView.onFailed(code, msg);
            }
        });
    }


    /**
     * 上传单张图片 提交任务
     */
    public void tijiao(File file, final String type) {
        MultipartBody.Builder builder = new MultipartBody.Builder().setType(MultipartBody.FORM);
        RequestBody imageBody = RequestBody.create(MediaType.parse("multipart/form-data"), file);
        builder.addFormDataPart("file", file.getName(), imageBody);
        List<MultipartBody.Part> parts = builder.build().parts();
        addSubscription(iHttpUrl.upload(parts), new BaseSubscriber<UploadFileEntity>() {
            @Override
            public void onNext(UploadFileEntity entity) {
                if (100 == entity.getRecode()) {
                    iView.onSuccess("" + entity.getRecode(), entity.getFilepath(), entity.getFilepath(), type);
                } else {
                    iView.onFailed("" + entity.getRecode(), entity.getFilepath());
                }
            }

            @Override
            public void onFailed(String code, String msg) {
                iView.onFailed(code, msg);
            }
        });
    }



    /**
     * 上传多张图片
     */
    public void uploadFiles(ArrayList<ImageItem> selImageList, final String type) {
        MultipartBody.Builder builder = new MultipartBody.Builder().setType(MultipartBody.FORM);
        if (selImageList != null && selImageList.size() != 0) {
            for (ImageItem s : selImageList) {
                File file = new File(s.getPath());
                RequestBody imageBody = RequestBody.create(MediaType.parse("multipart/form-data"), file);
                builder.addFormDataPart("file", file.getName(), imageBody);
            }
        }
        List<MultipartBody.Part> parts = builder.build().parts();
        addSubscription(iHttpUrl.uploadFiles(parts), new BaseSubscriber<List<UploadFilesEntity>>() {
            @Override
            public void onNext(List<UploadFilesEntity> entity) {
                iView.onSuccess("100", "上传图片成功", entity, type);
            }

            @Override
            public void onFailed(String code, String msg) {
                iView.onFailed(code, msg);
            }
        });
    }

    /**
     * 多图
     */
    public void uploadFiles2(ArrayList<ImageItem> selImageList, final String type) {
        MultipartBody.Builder builder = new MultipartBody.Builder().setType(MultipartBody.FORM);
        if (selImageList != null && selImageList.size() != 0) {
            for (ImageItem s : selImageList) {
                File file = new File(s.getPath());
                RequestBody imageBody = RequestBody.create(MediaType.parse("multipart/form-data"), file);
                builder.addFormDataPart("file", file.getName(), imageBody);
            }
        }
        List<MultipartBody.Part> parts = builder.build().parts();
        addSubscription(iHttpUrl.uploadFiles2(parts), new BaseSubscriber<List<UploadFilesEntity>>() {
            @Override
            public void onNext(List<UploadFilesEntity> entity) {
                iView.onSuccess("100", "上传图片成功", entity, type);
            }

            @Override
            public void onFailed(String code, String msg) {
                iView.onFailed(code, msg);
            }
        });
    }

    /**
     * 意见反馈
     */
    public void feedBack(String userid, String text, final String type) {
        addSubscription(iHttpUrl.feedback(userid, text), new BaseSubscriber<BaseEntity>() {
            @Override
            public void onNext(BaseEntity entity) {
                if ("100".equals(entity.getCode())) {
                    iView.onSuccess(entity.getCode(), entity.getRinfo(), entity.getRinfo(), type);
                } else {
                    iView.onFailed(entity.getCode(), entity.getRinfo());
                }
            }

            @Override
            public void onFailed(String code, String msg) {
                iView.onFailed(code, msg);
            }
        });
    }

}
